--[[
    {
        nodeType:(1:BMFont 2:Label)
        num:显示的数字,
        up:从下到上滚动
        fontName：字体
        showPlus：是否显示+
        disTime:每个数字之间延时滚动
        rollTime：总滚动时间
        cbk:滚动完之后的回调函数
    }
]]

local RoundNum = class("RoundNum",function()
    return display.newNode();
end);

local function getIntPart(x)
    if x <= 0 then
        return math.ceil(x);
    end
    if math.ceil(x) == x then
        x = math.ceil(x);
    else
        x = math.ceil(x) - 1;
    end
    return x;
end

local function dumpNum(num)
    local tmpNums = {};
    repeat
        local t = num % 10;
        table.insert(tmpNums,t);
        num = getIntPart(num / 10);
    until num <= 0;
    return tmpNums;
end

local function createTmpNode(nodeType,str,fontName)
    local tmpLabel = nil;
    if nodeType == 1 then
        tmpLabel = display.newBMFontLabel({text = str,font = fontName});
    else
        tmpLabel = display.newTTFLabel({text = str,font = fontName});    
    end
    return tmpLabel;
end



function RoundNum:ctor(target)
    local nodeType = target.type or 1;
    local num = target.num;
    local up = target.up or true;
    local fontName = nil;
    if nodeType == 1 then
        fontName = target.fontName or "fnt/011.fnt";
    else
        fontName = target.fontName or "Arial.ttf";
    end
    local showPlus = target.showPlus or false;
    local disTime = target.disTime or 0.2;
    local rollTime = target.rollTime or 1;
    local rol = target.rol or 2;
    local cbk = target.cbk;

    local tmpLabel = createTmpNode(nodeType,"0",fontName);
    local tmpSize = tmpLabel:getContentSize();

    local allNum = dumpNum(num);
    local count = (showPlus and #allNum + 1 or #allNum);
    self:setContentSize(cc.size(tmpSize.width * count,tmpSize.height));
    local tmpNodeArray = {};

    if showPlus then
        local plus = createTmpNode(nodeType,"+",fontName);
        plus:setAnchorPoint(cc.p(0,0));
        table.insert(tmpNodeArray,plus);
    end
    
    for i = #allNum,1,-1  do
        local node = display.newNode();
        node:setContentSize(cc.size(tmpSize.width,tmpSize.height));
        table.insert(tmpNodeArray,node);

        local allCount = allNum[i] + 10 * rol;
        local rect = nil;
        local clippingNode = display.newClippingRectangleNode(cc.rect(0,0,tmpSize.width,tmpSize.height));
        node:addChild(clippingNode); 

        local tmpNode = display.newNode();
        tmpNode:setContentSize(cc.size(tmpSize.width,tmpSize.height * allCount));
        clippingNode:addChild(tmpNode);


        for j = 0, allCount do
           local x = createTmpNode(nodeType,j%10,fontName);
           x:setAnchorPoint(cc.p(0,0));
           if up then
               x:setPosition(cc.p(0,-j * tmpSize.height));
           else
               x:setPosition(cc.p(0,j * tmpSize.height));
           end
           tmpNode:addChild(x);
        end
        if up then
            mgrs.action:beginAction(tmpNode):DelayTime(disTime * (#allNum - i + 1)):MoveBy(rollTime,cc.p(0,tmpSize.height * allCount)):endAction();
        else
            mgrs.action:beginAction(tmpNode):DelayTime(disTime * (#allNum - i + 1)):MoveBy(rollTime,cc.p(0,-tmpSize.height * allCount)):endAction();
        end
    end
    mgrs.action:beginAction(self):DelayTime(disTime * (#allNum + 1) + rollTime):CallFunc(function()
        if cbk then
            cbk();
        end
    end):endAction();


    for key, var in ipairs(tmpNodeArray) do
        var:setPosition(cc.p((key - 1)*tmpSize.width,0));
        self:addChild(var);
    end

end

return RoundNum;
--endregion
